<template>
  <view v-if="goods_info.goods_name">
    <!-- 商品详情轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
      <swiper-item v-for="(item, i) in goods_info.pics" :key="i">
        <image class="swiper-item" :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="goods-price">
        ¥{{goods_info.goods_price}}
      </view>
      <!-- 商品信息主体区 -->
      <view class="goods-info-body">
        <view class="goods-name">{{goods_info.goods_name}}</view>
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="yf">快递：免运费</view>
    </view>
    <!-- 商品详情区域 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 商品导航组件  -->
    <view class="goods-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick"></uni-goods-nav>
    </view>
  </view>
</template>

<script>
  import { mapState, mapMutations, mapGetters } from 'vuex'
  export default {
    // 页面数据
    data() {
      return {
        // 商品详情
        goods_info: {},
        // 购物车左边配置
        options: [{
          icon: 'shop',
          text: '店铺'
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        // 购物车右边按钮
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }]
      };
    },
    // 页面计算属性
    computed:{
      // 调用vuex的mapState辅助函数，将cart模块中的cart数据map到当前组件
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['total']),
    },
    // 配置页面属性监听器
    watch:{
      // 监听 total 
      // total(newVal){
      //   this.options[1].info = newVal
      // }
      total: {
        handler(newVal){
          this.options[1].info = newVal
        },
      // immediate 属性用来声明此侦听器，是否在页面初次加载完毕后立即调用
      immediate: true
      }
    },
    // 页面加载后
    onLoad(options) {
      // 获取商品id
      const goods_id = options.goods_id
      // 获取商品详情
      this.getGoodsDetail(goods_id)
      
      // 初始化购物车数据
      // this.options[1].info = this.total
      // console.log('测试cart数据是否映射成功', this.cart)
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      // 获取商品详情
      // https://api-hmugo-web.itheima.net/api/public/v1/goods/detail
      async getGoodsDetail(goods_id) {
        //发起请求
        const {
          data: res
        } = await uni.$http.get('/goods/detail', {
          goods_id: goods_id
        })
        console.log('subpkg/goods_detail/goods_detail.vue/goods_info res', res)
        // 请求异常判断
        if (res.meta.status !== 200) return uni.$showMsg()
        // 设置data
        // 解决图片底部白边问题，解决webp格式图片iOS设备无法显示问题
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')
          .replace(/webp/g, 'jpg')

        this.goods_info = res.message
      },
      // 轮播图预览
      preview(i) {
        uni.previewImage({
          current: i,
          urls: this.goods_info.pics.map(item => item.pics_big)
        })
      },
      // 底部导航左侧按钮点击
      onClick(e){
        console.log('左侧',e)
        if(e.content.text === "购物车"){
          uni.switchTab({
            url: "/pages/cart/cart"
          })
        }
      },
      // 底部导航右侧按钮点击
      buttonClick(e){
        console.log('右侧',e)
        if(e.content.text === "加入购物车"){
          //将购物车数据存储到cart store中
          // { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
          // 构造商品对象
          const goods = {
            goods_id: this.goods_info.goods_id,
            goods_name: this.goods_info.goods_name,
            goods_price: this.goods_info.goods_price,
            goods_count: 1, // 详情页一次添加一个商品
            goods_small_logo: this.goods_info.goods_small_logo,
            goods_state: true, // 商品勾选状态，在购物车列表页使用
          }
          // 保存商品数据到store中的cart
          this.addToCart(goods)
        }
      }
      
    }
  }
</script>

<style lang="scss">
  // 轮播图
  swiper {
    height: 750rpx;

    image {
      height: 100%;
      width: 100%;
    }
  }

  // 商品详情区
  .goods-info-box {
    padding: 10px;

    .goods-price {
      color: #c00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        padding-right: 10px;
      }

      .favi {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }

    .yf {
      margin: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }

  // 商品导航
  .goods-nav {
    position: sticky;
    bottom: 0;
    z-index: 99;
  }
</style>
